package com.kunbo.cn.app.controller;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
//import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.app.service.DeliveryNoticeService;
import com.kunbo.cn.entity.core.card.IssuanceCard;
import com.kunbo.cn.entity.core.confirm.ConfirmRecord;
import com.kunbo.cn.entity.core.handset.HandsetUsers;
import com.kunbo.cn.entity.core.poundlist.PurchasePoundList;
import com.kunbo.cn.entity.core.poundlist.SalesPoundList;
import com.kunbo.cn.enumeration.core.poundlist.PoundListState;
import com.kunbo.cn.service.core.card.IssuanceCardService;
import com.kunbo.cn.service.core.confirm.ConfirmRecordService;
import com.kunbo.cn.service.core.handset.HandsetUsersService;
import com.kunbo.cn.service.core.poundlist.PurchasePoundListService;
import com.kunbo.cn.service.core.poundlist.SalesPoundListService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.json.FastJsonUtils;
import com.kunbo.cn.util.retu.BuildBean;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.ResultBean;

/**
 * 收发货管理app
 * @author  编写人:刘义
 * @date 时间:2019年3月18日上午10:56:03
 */
@RestController
@RequestMapping("/delivery")
public class DeliveryManagementApp {
	
	
	@Autowired
	private DeliveryNoticeService service;
	
	@Autowired
	private IssuanceCardService icCardService;
	
	@Autowired
	private PurchasePoundListService ppls;
	
	@Autowired
	private SalesPoundListService spls;
	@Autowired
	private ConfirmRecordService crService;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private HandsetUsersService huService;
	
	/**
	 * 收货管理
	 * @param idCard
	 * @return
	 */
	@RequestMapping("/platformConfirmation")
	public ResultBean<Boolean> platformConfirmation(
			@RequestParam("idCard")String idCard,
			@RequestParam("primary")Double primary,//原发
			@RequestParam("weightDeduction")Double weightDeduction,//扣重
			@RequestParam("buckle")Double buckle //扣杂
			){
		ResultBean<Boolean> result = new ResultBean<Boolean>();
		return BuildBean.buildResult(result, "success", "error", service.selCard(idCard,primary,weightDeduction,buckle));
	}
	
	
	
	/**
	 * 根据身份证号进行一二次确认
	 * @param idCard
	 * @return
	 */
	@PostMapping("/confirm")
	public ResultBean<List<Map<String,Object>>> confirm(
			@RequestParam("idCard")String idCard,
			@RequestParam("tableName")String tableName
			){
		ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
		return BuildBean.buildResult(result, "success", "error", service.platformConfirmation(tableName,idCard));
	}
	//F3 00 5A CE
	//收货确认接口
	@PostMapping("/getFirstReceiveData")
	public CommonResult getFirstReceiveData(String icCardNumber) {
		if(StringUtils.isBlank(icCardNumber)) {
			return new CommonResult(false,"IC卡号为空",null);
		}
		//根据ic卡号获取对应的身份证号
		System.out.println("当前卡号为："+icCardNumber);
		String idNumber  = "";
		List<Map<String,Object>> cardList = icCardService.findByIdCardNumber(icCardNumber);
		if(cardList.size()>0) {
			idNumber = cardList.get(0).get("id_card").toString();
		}
		PurchasePoundList ppl = ppls.findByIDNumber(idNumber,PoundListState.firstWeigh);
		if(null==ppl) {
			return new CommonResult(false,"对不起没有需要确认的派车单",null);
		}
		return new CommonResult(true,"获取成功",ppl);
		
	}
	
	@PostMapping("/getSecondReceiveData")
	public CommonResult getSecondReceiveData(String icCardNumber) {
		if(StringUtils.isBlank(icCardNumber)) {
			return new CommonResult(false,"IC卡号为空",null);
		}
		//根据ic卡号获取对应的身份证号
		System.out.println("当前卡号为："+icCardNumber);
		String idNumber  = "";
		List<Map<String,Object>> cardList = icCardService.findByIdCardNumber(icCardNumber);
		if(cardList.size()>0) {
			idNumber = cardList.get(0).get("id_card").toString();
		}
		PurchasePoundList ppl = ppls.findByIDNumber(idNumber,PoundListState.firstConfirm);
		if(null==ppl) {
			return new CommonResult(false,"对不起没有需要确认的派车单",null);
		}
		return new CommonResult(true,"获取成功",ppl);
		
	}
	//首次确认state传3，二次确认state传4
	@Transactional
	@PostMapping("/recrivedGoodFirstConfirm")
	public CommonResult recrivedGoodConfirm(String uuid,String confirmPerson) {
		PurchasePoundList ppl = ppls.findById(uuid);
		//判断该用户是否有确认该物料的权限
		if(!ifPermitConfirm(ppl.getMateriel(),confirmPerson)) {
			return new CommonResult(false,"对不起，您没有确认该物料的权限",null);
		}
		ppl.setHandset_confirm(confirmPerson);
		ppl.setState(PoundListState.firstConfirm);
		ConfirmRecord cr = new ConfirmRecord();
		cr.setConfirmType("采购首次确认");
		cr.setPoundListNumber(ppl.getCgPoundNumber());
		cr.setConfirmPerson(confirmPerson);
		cr.setConfirmTime(SDate.getSystemDateToString());
		ppls.receiveConfirm(ppl);
		crService.save(cr);
		return new CommonResult(true,"收货首次确认成功",null);
		
	}
	@Transactional
	@PostMapping("/recrivedGoodSecondConfirm")
	public CommonResult recrivedGoodSecondConfirm(String uuid,String confirmPerson) {
		PurchasePoundList ppl = ppls.findById(uuid);
		if(!ifPermitConfirm(ppl.getMateriel(),confirmPerson)) {
			return new CommonResult(false,"对不起，您没有确认该物料的权限",null);
		}
		ppl.setHandset_confirm(confirmPerson);
		ppl.setState(PoundListState.secondConfirm);
		ConfirmRecord cr = new ConfirmRecord();
		cr.setConfirmType("采购二次确认");
		cr.setPoundListNumber(ppl.getCgPoundNumber());
		cr.setConfirmPerson(confirmPerson);
		cr.setConfirmTime(SDate.getSystemDateToString());
		ppls.receiveConfirm(ppl);
		crService.save(cr);
		return new CommonResult(true,"收货二次确认成功",null);
		
	}
	
	/**-------发货确认接口----------------------------------*/
	//收货确认接口
		@PostMapping("/getFirstSendData")
		public CommonResult getFirstSendData(String icCardNumber) {
			if(StringUtils.isBlank(icCardNumber)) {
				return new CommonResult(false,"IC卡号为空",null);
			}
			//根据ic卡号获取对应的身份证号
			System.out.println("当前卡号为："+icCardNumber);
			String idNumber  = "";
			List<Map<String,Object>> cardList = icCardService.findByIdCardNumber(icCardNumber);
			if(cardList.size()>0) {
				idNumber = cardList.get(0).get("id_card").toString();
			}
			SalesPoundList spl = spls.findByIdCard(idNumber,PoundListState.firstWeigh);

			if(null==spl) {
				return new CommonResult(false,"对不起没有需要确认的派车单",null);
			}
			return new CommonResult(true,"获取成功",spl);
			
		}
		
		@PostMapping("/getSecondSendData")
		public CommonResult getSecondSendData(String icCardNumber) {
			if(StringUtils.isBlank(icCardNumber)) {
				return new CommonResult(false,"IC卡号为空",null);
			}
			//根据ic卡号获取对应的身份证号
			System.out.println("当前卡号为："+icCardNumber);
			String idNumber  = "";
			List<Map<String,Object>> cardList = icCardService.findByIdCardNumber(icCardNumber);
			if(cardList.size()>0) {
				idNumber = cardList.get(0).get("id_card").toString();
			}
			SalesPoundList spl = spls.findByIdCard(idNumber,PoundListState.firstConfirm);
			if(null==spl) {
				return new CommonResult(false,"对不起没有需要确认的派车单",null);
			}
			return new CommonResult(true,"获取成功",spl);
			
		}
		
		
		//首次确认state传3，二次确认state传4
		@Transactional
		@PostMapping("/sendGoodFirstConfirm")
		public CommonResult sendGoodFirstConfirm(String uuid,String confirmPerson) {
			SalesPoundList spl = spls.findById(uuid);
			if(!ifPermitConfirm(spl.getMateriel(),confirmPerson)) {
				return new CommonResult(false,"对不起，您没有确认该物料的权限",null);
			}
			spl.setHandset_confirm(confirmPerson);
			spl.setState(PoundListState.firstConfirm);
			ConfirmRecord cr = new ConfirmRecord();
			cr.setConfirmType("销售首次确认");
			cr.setPoundListNumber(spl.getXsPoundNumber());
			cr.setConfirmPerson(confirmPerson);
			cr.setConfirmTime(SDate.getSystemDateToString());
			spls.sendConfirm(spl);
			crService.save(cr);
			return new CommonResult(true,"发货首次确认成功",null);
			
		}
		
		//首次确认state传3，二次确认state传4
		@PostMapping("/sendGoodSecondConfirm")
		public CommonResult sendGoodSecondConfirm(String uuid,String confirmPerson) {
			SalesPoundList spl = spls.findById(uuid);
			if(!ifPermitConfirm(spl.getMateriel(),confirmPerson)) {
				return new CommonResult(false,"对不起，您没有确认该物料的权限",null);
			}
			spl.setHandset_confirm(confirmPerson);
			spl.setState(PoundListState.secondConfirm);
			ConfirmRecord cr = new ConfirmRecord();
			cr.setConfirmType("销售二次确认");
			cr.setPoundListNumber(spl.getXsPoundNumber());
			cr.setConfirmPerson(confirmPerson);
			cr.setConfirmTime(SDate.getSystemDateToString());
			spls.sendConfirm(spl);
			crService.save(cr);
			return new CommonResult(true,"发货二次确认成功",null);
			
		}
		
		//判断手持机用户是否有确认物料的权限
		public boolean ifPermitConfirm(String materialName,String confirmPerson) {
			//根据物料名称获取对应的id
			String sql = "select id,name from  business_materials_manage where name = '"+materialName+"'";
			List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
			String mId = list.get(0).get("id").toString();
		    //根据确认人获取对应的确认权限
			HandsetUsers hu = huService.findByName(confirmPerson);
		    //进行比对
			String materialAuthority = hu.getConfirm_matter();
			String[] strs=materialAuthority.split(",");
			for(int i=0;i<strs.length;i++) {
				if(strs[i].indexOf(mId)!=-1){//循环查找字符串数组中的每个字符串中是否包含所有查找的内容
					return true;//查找到了就返回真，不在继续查询
					}
			}
			return false;
			
		}
		
		
	
}
